package com.cx.property;

import com.cx.utils.Const;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.util.StringUtils;
import redis.clients.jedis.JedisShardInfo;

import java.util.Arrays;
import java.util.List;

@ConfigurationProperties(
        prefix = "spring.redis"
)
public class JedisProperties extends RedisProperties {

    /**
     * 项目名称
     */
    private String proName = "njr";

    /**
     * 密码（重写防止无密码报错）
     */
    private String password;

    /**
     * 一级缓存名称
     */
    private String cacheLevel1 = "cache1st";

    /**
     * 二级缓存名称
     */
    private String cacheLevel2 = "cache2nd";

    /**
     * 一级缓存过期时间
     */
    private Long cacheLevel1Ttl = 345600L; //过期时间4天

    /**
     * 二级缓存过期时间
     */
    private Long cacheLevel2Ttl = -1L;

    private JedisProperties.Pool pool;

    /**
     * 分片: 第一台
     */
    private JedisShardInfo shardInfo;

    /**
     * 分片：后三台
     */
    private JedisProperties.Shard shard;

    public JedisProperties() {
        super();
    }

    public JedisProperties.Pool getPool() {
        return pool;
    }

    public void setPool(JedisProperties.Pool pool) {
        this.pool = pool;
    }

    public static class Pool extends RedisProperties.Pool {
        private boolean testOnBorrow = false;
        private boolean testOnReturn = false;
        private boolean testWhileIdle = false;
        private long timeBetweenEvictionRunsMillis = -1L;
        private int numTestsPerEvictionRun = 3;
        private long minEvictableIdleTimeMillis = 1800000L;

        public Pool() {
        }

        public boolean isTestOnBorrow() {
            return testOnBorrow;
        }

        public void setTestOnBorrow(boolean testOnBorrow) {
            this.testOnBorrow = testOnBorrow;
        }

        public boolean isTestOnReturn() {
            return testOnReturn;
        }

        public void setTestOnReturn(boolean testOnReturn) {
            this.testOnReturn = testOnReturn;
        }

        public boolean isTestWhileIdle() {
            return testWhileIdle;
        }

        public void setTestWhileIdle(boolean testWhileIdle) {
            this.testWhileIdle = testWhileIdle;
        }

        public long getTimeBetweenEvictionRunsMillis() {
            return timeBetweenEvictionRunsMillis;
        }

        public void setTimeBetweenEvictionRunsMillis(long timeBetweenEvictionRunsMillis) {
            this.timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis;
        }

        public int getNumTestsPerEvictionRun() {
            return numTestsPerEvictionRun;
        }

        public void setNumTestsPerEvictionRun(int numTestsPerEvictionRun) {
            this.numTestsPerEvictionRun = numTestsPerEvictionRun;
        }

        public long getMinEvictableIdleTimeMillis() {
            return minEvictableIdleTimeMillis;
        }

        public void setMinEvictableIdleTimeMillis(long minEvictableIdleTimeMillis) {
            this.minEvictableIdleTimeMillis = minEvictableIdleTimeMillis;
        }
    }

    public JedisShardInfo getShardInfo() {
        return shardInfo;
    }

    public void setShardInfo(JedisShardInfo shardInfo) {
        this.shardInfo = shardInfo;
    }

    /**
     * 分片模式：加载配置时，其实只要多数节点配置上就可以，其它自己发现
     * 注意：info(1～3)都不能为空
     */
    public static class Shard {
        private JedisShardInfo info1;
        private JedisShardInfo info2;
        private JedisShardInfo info3;

        public Shard() {
        }

        public JedisShardInfo getInfo1() {
            return info1;
        }

        public void setInfo1(JedisShardInfo info1) {
            this.info1 = info1;
        }

        public JedisShardInfo getInfo2() {
            return info2;
        }

        public void setInfo2(JedisShardInfo info2) {
            this.info2 = info2;
        }

        public JedisShardInfo getInfo3() {
            return info3;
        }

        public void setInfo3(JedisShardInfo info3) {
            this.info3 = info3;
        }
    }

    public String getProName() {
        return proName;
    }

    public void setProName(String proName) {
        this.proName = proName;
    }

    public String getPassword() {
        return StringUtils.hasLength(this.password) ? this.password : null;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getCacheLevel1() {
        return cacheLevel1;
    }

    public void setCacheLevel1(String cacheLevel1) {
        this.cacheLevel1 = cacheLevel1;
    }

    public String getCacheLevel2() {
        return cacheLevel2;
    }

    public void setCacheLevel2(String cacheLevel2) {
        this.cacheLevel2 = cacheLevel2;
    }

    public Long getCacheLevel1Ttl() {
        return cacheLevel1Ttl;
    }

    public void setCacheLevel1Ttl(Long cacheLevel1Ttl) {
        this.cacheLevel1Ttl = cacheLevel1Ttl;
    }

    public Long getCacheLevel2Ttl() {
        return cacheLevel2Ttl;
    }

    public void setCacheLevel2Ttl(Long cacheLevel2Ttl) {
        this.cacheLevel2Ttl = cacheLevel2Ttl;
    }

    public Shard getShard() {
        return shard;
    }

    public void setShard(Shard shard) {
        this.shard = shard;
    }

    public List<JedisShardInfo> shards(){
        return Arrays.asList(
                shard.getInfo1(),
                shard.getInfo2(),
                shard.getInfo3());
    }

    public String cacheLevel1Pre(){
        return this.proName + Const.SP + this.cacheLevel1 + Const.SP;
    }

    public String cacheLevel2Pre(){
        return this.proName + Const.SP + this.cacheLevel2 + Const.SP;
    }
}